﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Xml;



public class CustomXmlNode
{
    private XmlNode m_Node;
    private List<CustomXmlNode> m_children;

    public List<CustomXmlNode> Children { get { return m_children; } }

    public XmlNode CurrentNode { get { return m_Node; }}

    public CustomXmlNode(XmlNode data)
    {
        m_Node = data;
        m_children = new List<CustomXmlNode>();
        GetChilds();
    }

    /// <summary>
    /// 获取某个属性值
    /// </summary>
    /// <param name="at"></param>
    /// <returns></returns>
    public string GetAttribute(string at)
    {
        if (m_Node == null) return null;
        XmlElement ele = (XmlElement)m_Node;
        if (ele.HasAttribute(at))
        {
            return ele.GetAttribute(at);
        }
        else
            return null;
    }

    private void GetChilds()
    {
        if (m_Node == null) return;
        foreach (XmlNode node in m_Node.ChildNodes)
        {
            CustomXmlNode tem = new CustomXmlNode(node);
            m_children.Add(tem);
        }
    }
}

/// <summary>
/// xml解析脚本
/// </summary>
public class XmlParser {

    public CustomXmlNode Root { get; private set; }

    public bool ReadData(string path,string rootname)
    {
        XmlDocument xmlD = new XmlDocument();

        try
        {
            xmlD.Load(path);
        }
        catch
        {
            return false;

        }


        XmlNode root = xmlD.SelectSingleNode(rootname);
        Root = new CustomXmlNode(root);
        return true;
    }
}
